//
//  AppDelegate.swift
//  weibo
//
//  Created by 吴婷婷 on 15/11/3.
//  Copyright © 2015年 wutingting. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewController
        window?.makeKeyAndVisible()
        
        //监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, queue: nil)
            {
            (notification) -> Void in
                let vc = notification.object != nil ? WelcomeViewController() : MainViewController()
                self.window?.rootViewController = vc
        }

        return true
    }
    //注销通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: WBSwitchRootViewControllerNotification, object: nil)
    }

    private func setUpApprance(){
        UITabBar.appearance().tintColor = WBAppearanceTintColor
        UINavigationBar.appearance().tintColor = WBAppearanceTintColor
    }
}


extension AppDelegate {
    
    ///启动的根视图控制器
    private var defaultRootViewController: UIViewController {
        
        if UserAccountViewModel.sharedUserAccount.userLogon {
            return isNewVerson ? NewFeatureCollectionViewController() : WelcomeViewController()
        }
        
        return MainViewController()
    }
    
    ///判断是否新版本
    private var isNewVerson: Bool {
        
        let sanboxVersonkey = "sanboxVersonkey"
        let sandboxVerson = NSUserDefaults.standardUserDefaults().doubleForKey(sanboxVersonkey)
        
//        print(NSBundle.mainBundle().infoDictionary)
        let currentVerson = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let verson = Double(currentVerson)!
        
        NSUserDefaults.standardUserDefaults().setDouble(verson, forKey: sanboxVersonkey)
        
        return verson > sandboxVerson
    }
    
    
}
